<form id="searchForm" action="<?php echo current_url()?>">
	
	<label>
		ค้นหาเดือนปี
	</label>
	<div id="" class="dates">
		<input type="text"  placeholder="วันเริ่มโปรโมชั่น" class="inputText fromDate" name="fromDate" value="<? echo (isset($_GET['fromDate'])) ? $_GET['fromDate']:""?>" />
		to     
		<input type="text" placeholder="วันสิ้นสุดโปรโมชั่น" class="inputText endDate" name="endDate" value="<? echo (isset($_GET['endDate'])) ? $_GET['endDate']:""?>" />
   
    	<button type="button" id="clear">Clear</button>
   </div>
  
   <br />
	<h4>เลือกรายการ</h4><br />
	
	<div class="clear"></div>
    <strong><h4>ประเภทการขาย</h4></strong>
	<?
	$arraySearch[] = array("key"=>"เครดิต","value"=>"01");
	$arraySearch[] = array("key"=>"หน่วยรถ","value"=>"02");
	foreach($arraySearch as $in=>$val){
		$checked = (isset($_GET['SALE_TYPE']) && $_GET['SALE_TYPE']==$val['value']) ? 'checked="checked"' :"";
	?>
	<label>
		<input <?php echo $checked?> type="radio" class="inputText" name="SALE_TYPE" value="<?php echo $val['value']?>" />
		<?php echo $val['key']?>
	</label>
	<?
	}
	?>
	
	<div class="clear"></div>
	<strong><h4>ประเภทโปรโมชั่น</h4></strong>
	<?
	$arraySearch = array();
	$arraySearch[] = array("key"=>"โปรโมชั่นปกติ","value"=>"NB");
	$arraySearch[] = array("key"=>"โปรโมชั่นพิเศษ","value"=>"SP");
	$arraySearch[] = array("key"=>"โปรโมชั่นรายลูกค้า","value"=>"CT");
	foreach($arraySearch as $in=>$val){
		$checked = (isset($_GET['PROMO_TYPE']) && $_GET['PROMO_TYPE']==$val['value']) ? 'checked="checked"' :"";
	?>
	<label>
		<input <?php echo $checked?> type="radio" class="inputText" name="PROMO_TYPE" value="<?php echo $val['value']?>" />
		<?php echo $val['key']?>
	</label>
	
	<?
	}
	?>
	
	
	<!--<label>
		<input type="radio" name="base_unit" value="CAR"  /> หน่วยรถ
		<input type="radio" name="base_unit" value="EA"  /> เครดิต
	</label> -->
	<div class="clear"></div>
	<input type="submit" value="ค้นหา" />
</form>
<div class=" pagination">
        <ul class="pagination">
            <?
            echo $pagination->create_links();
            ?>
        </ul>    
    </div>

<table class="table table-striped table-bordered table-condensed"> <!-- cellspacing='0' is important, must stay -->

	<!-- Table Header -->
	<thead>
		<tr>
			<th style="width:10%">ID</th>
			<th style="width:30%">รายละเอียดโปรโมชั่น</th>
			<th style="width:20%">ระยะเวลา</th>
            <th style="width:10%">รายการสินค้าขาย</th>
            <th style="width:10%">รายการสินค้าแถม</th>
            <th style="width:10%">จำนวนซื้อ/ของแถม/ส่วนลด</th>
			
		</tr>
	</thead>
	<!-- Table Header -->
	
	<!-- Table Body -->
	<tbody>
<?
	if(!empty($dataList)){
	?>
		<?
		
		foreach($dataList as $in=>$val){
		?>
			<tr>
				<td><?php echo $val->PROMO_ID?></td>
				<td><?php echo $val->PROMO_DESC?></td>
				<td>
				<?php echo DateThai($val->START_DATE)?> - 
				<?php if($val->END_DATE="9999-12-31"){echo "ไม่มีกำหนด";}else{echo DateThai($val->END_DATE);}?>
                </td>
                <td><a target="_blank" href="<?php echo $current?>viewsmat/<?php echo $val->PROMO_ID?>">รายละเอียด</a></td>
                <td><a target="_blank" href="<?php echo $current?>viewfmat/<?php echo $val->PROMO_ID?>">รายละเอียด</a></td>
                <td><a target="_blank" href="<?php echo $current?>viewpromolevel/<?php echo $val->PROMO_ID?>">รายละเอียด</a></td>
				
			</tr>
		<?
		}
		?>
<?
	}else{
	?>
		<tr>
			<td colspan="11"><h2>ไม่พบข้อมูล</h2></td>
		</tr>
	<?
	}
	?>
	
	</tbody>
	
	<!-- Table Body -->

</table>
<div class=" pagination">
        <ul class="pagination">
            <?
            echo $pagination->create_links();
            ?>
        </ul>    
    </div>
    
<script>

$.datepicker._defaults.onAfterUpdate = null;

var datepicker__updateDatepicker = $.datepicker._updateDatepicker;
$.datepicker._updateDatepicker = function( inst ) {
   datepicker__updateDatepicker.call( this, inst );

   var onAfterUpdate = this._get(inst, 'onAfterUpdate');
   if (onAfterUpdate)
      onAfterUpdate.apply((inst.input ? inst.input[0] : null),
         [(inst.input ? inst.input.val() : ''), inst]);
}


$(function() {
	
	
	 $( ".fromDate" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
      numberOfMonths: 3,
      dateFormat:'yy-mm-dd',
      onClose: function( selectedDate ) {
        $( ".endDate" ).datepicker( "option", "minDate", selectedDate );
      }
    });
    $( ".endDate" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
      numberOfMonths: 3,
      dateFormat:'yy-mm-dd',
      onClose: function( selectedDate ) {
        $( ".fromDate" ).datepicker( "option", "maxDate", selectedDate );
      }
    });
	
	
	$('#clear').click(function(){
		$('.fromDate,.endDate').val("");
		
	})
   var cur = -1, prv = -1;
   $('#jrange div')
      .datepicker({
            //numberOfMonths: 3,
            changeMonth: true,
            changeYear: true,
            showButtonPanel: true,
			 isBE: true,
            autoConversionField: true,
            beforeShowDay: function ( date ) {
                  return [true, ( (date.getTime() >= Math.min(prv, cur) && date.getTime() <= Math.max(prv, cur)) ? 'date-range-selected' : '')];
               },

            onSelect: function ( dateText, inst ) {
                  var d1, d2;

                  prv = cur;
                  cur = (new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay)).getTime();
                  if ( prv == -1 || prv == cur ) {
                     prv = cur;
                     $('#jrange input').val( dateText );
                  } else {
                     d1 = $.datepicker.formatDate( 'dd/mm/yy', new Date(Math.min(prv,cur)), {} );
                     d2 = $.datepicker.formatDate( 'dd/mm/yy', new Date(Math.max(prv,cur)), {} );
                     $('#jrange input').val( d1+' - '+d2 );
                  }
               },

            onChangeMonthYear: function ( year, month, inst ) {
                  //prv = cur = -1;
               },

            onAfterUpdate: function ( inst ) {
                  $('<button type="button" class="ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all" data-handler="hide" data-event="click">Done</button>')
                     .appendTo($('#jrange div .ui-datepicker-buttonpane'))
                     .on('click', function () { $('#jrange div').hide(); });
               }
         })
      .position({
            my: 'left top',
            at: 'left bottom',
            of: $('#jrange input')
         })
      .hide();

   $('#jrange input').on('focus', function (e) {
         var v = this.value,
             d;

         try {
            if ( v.indexOf(' - ') > -1 ) {
               d = v.split(' - ');

               prv = $.datepicker.parseDate( 'mm/dd/yy', d[0] ).getTime();
               cur = $.datepicker.parseDate( 'mm/dd/yy', d[1] ).getTime();

            } else if ( v.length > 0 ) {
               prv = cur = $.datepicker.parseDate( 'mm/dd/yy', v ).getTime();
            }
         } catch ( e ) {
            cur = prv = -1;
         }

         if ( cur > -1 )
            $('#jrange div').datepicker('setDate', new Date(cur));

		$('#jrange div').css({"position":"absolute","top":"15px"});
         $('#jrange div').datepicker('refresh').show();
         
      });

});

</script>